package com.lu.edu.info.manager.controller;

import com.lu.edu.info.manager.domain.Student;
import com.lu.edu.info.manager.service.StudentService;

import java.util.Scanner;

/**
 * @author lufeng
 * @date 2021/9/14 10:51
 */

/*
 * 客服接待
 */
public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    public void start() {

        studentloop:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("请输入您的选择：1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
//                    System.out.println("添加学生");
                    break;
                case "2":
                    deleteStudent();
//                    System.out.println("删除学生");
                    break;
                case "3":
                    setStudent();
//                    System.out.println("修改学生");
                    break;
                case "4":
                    findAllStudent();
//                    System.out.println("查看学生");
                    break;
                case "5":
                    System.out.println("感谢您的使用学生管理系统");

                    break studentloop;
                default:
                    System.out.println("您的输入有错，请重新输入");
                    break;
            }
        }
    }

    /**
     * 修改学生信息
     */
    public void setStudent() {
        String sid = inputStudentid();
        Student newstu = fengStudent(sid);
        studentService.setStudent(sid, newstu);
        System.out.println("修改成功");
    }

    /**
     * 查看学生信息
     */
    public void findAllStudent() {
        Student[] student = studentService.findAllStudent();
        if (student == null) {
            System.out.println("查无此信息");
            return;
        }
        System.out.println("学号" + "\t\t" + "姓名" + "\t" + "年龄" + "\t" + "生日");
        for (int i = 0; i < student.length; i++) {

            Student stu = student[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBrithday());
            }

        }

    }

    /**
     * 删除学生信息
     */
    public void deleteStudent() {
        String delID = inputStudentid();
        studentService.deleteStudent(delID);
        System.out.println("删除成功");

    }

    /**
     * 添加学生信息
     */
    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生学号");
            id = sc.next();
//           调用studentService中的isExists方法看是否存在id 设置一个布尔类型的变量falg来接收返回值
            boolean flag = studentService.isExists(id);
            if (flag == true) {
                System.out.println("学号已被注册，请重新输入");
            } else {

                break;
            }

        }
        Student stu = fengStudent(id);
//        通过stu调用studentService中的addStudebt方法看是否添加成功
        boolean result = studentService.addStudebt(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    /**
     * 输入id
     */
    public String inputStudentid() {
        String id;
        while (true) {
            System.out.println("请输入学生ID");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入");
            } else {
                break;
            }
        }
        return id;
    }
    /**
     * 添加封装学生
     */
    public Student fengStudent(String id){
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
//        将输入的学生信息进行封装stu对象
        Student stu = new Student(id, name, age, birthday);
        return stu;
    }
}

